<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${pkgName}.${tableMeta.modelName}Dao">

    <resultMap id="${tableMeta.modelName}Result" type="${tableMeta.modelName}">
    <#list tableMeta.columnMetas as x>
        <#if (x.name == tableMeta.primaryKey)>
            <id property="id" column="id" />
        <#else>
            <result property="${x.attrName}" column="${x.attrName}" />
        </#if>
    </#list>
        <!--
        一对多例子
		<collection property="roles" ofType="Role">
			<id property="id" column="roles.id" />
			<id property="name" column="roles.name" />
			<id property="description" column="roles.description" />
			<id property="available" column="roles.available" />
		</collection>
        多对一例子
		<association property="company" javaType="Company">
            <id property="id" column="company.id"/>
            <id property="name" column="company.name"/>
        </association>
		-->
    </resultMap>

    <sql id="${tableMeta.modelName}Columns">
    <#assign columnField>
        <#list tableMeta.columnMetas as x>
            ${x.name} As ${x.attrName},
        </#list>
    </#assign>
    ${columnField?substring(0, columnField?last_index_of(","))}
    </sql>

    <select id="get" resultType="${tableMeta.modelName}">
        SELECT
        <include refid="${tableMeta.modelName}Columns"/>
        FROM ${tableMeta.name} a
        WHERE a.${tableMeta.primaryKey} = ${"#"}{id}
    </select>


    <insert id="insert">
        INSERT INTO ${tableMeta.name}(
    <#assign insertField>
        <#list tableMeta.columnMetas as c>
            ${c.name},
        </#list>
    </#assign>
    ${insertField?substring(0, insertField?last_index_of(","))}
        ) VALUES (
    <#assign insertJavaField>
        <#list tableMeta.columnMetas as c>
            ${"#"}{${c.attrName}},
        </#list>
    </#assign>
    ${insertJavaField?substring(0, insertJavaField?last_index_of(","))}
        )
    </insert>

    <update id="update">
        UPDATE ${tableMeta.name} SET 1=1
        <#list tableMeta.columnMetas as c>
            <if test="${c.attrName} != null and ${c.attrName} != ''">
                ,${c.name} = ${"#"}{${c.attrName}}
            </if>
        </#list>
        WHERE ${tableMeta.primaryKey} = ${"#"}{id}
    </update>


    <update id="delete">
        DELETE FROM ${tableMeta.name} WHERE ${tableMeta.primaryKey} = ${"#"}{id}
    </update>


    <select id="findList" resultType="${tableMeta.modelName}">
        SELECT
        <include refid="${tableMeta.modelName}Columns"/>
        FROM ${tableMeta.name} a 1 = 1
    <#list tableMeta.columnMetas as c>
        <if test="${c.attrName} != null and ${c.attrName} != ''">
            AND ${c.name} = ${"#"}{${c.attrName}}
        </if>
    </#list>
    </select>
</mapper>